package healthyPointsManager.component
{
	
	import flash.events.MouseEvent;
	
	import mx.controls.AdvancedDataGrid;
	import mx.events.DragEvent;
	import mx.managers.DragManager;	

	public class UnscrollableDataGrid extends AdvancedDataGrid
	{			
		//Footer part
		public static const FOOTER_HEIGHT:int = 22;
		
		protected var footer:AdvancedDataGridFooter;
		//Unscrollable part
		private var _scrollEnabled:Boolean = true;
		
	
		public function UnscrollableDataGrid()
		{
			super();
			explicitRowHeight = 22;
		}	
		
		public function get scrollEnabled():Boolean
		{
			return _scrollEnabled;
		}
		
		public function set scrollEnabled(value:Boolean):void
		{
			_scrollEnabled = value;
		}
		
		override protected function dragScroll():void
		{
			if (_scrollEnabled)
			{
				super.dragScroll();
			}
		}
		/*Hides any errors that might occur when pressing a mouse down on this datagrid, not sure if this is really needed but too lazy to test it.*/
		override protected function mouseDownHandler(event:MouseEvent):void{
			try{
				super.mouseDownHandler(event);
			}catch(err:Error){
				return;
			}
		}
		
		/*Turns off all mouseOver handler function*/
		override protected function mouseOverHandler(event:MouseEvent):void{			
//			if(event.currentTarget.currentRowNum == 0){
//				return;
//			}else{
//				super.mouseOverHandler(event);
//			}
		}
		/*This prevents a drag from occuring when a user clicks on the header of this datagrid.  Otherwise a drag starts normally*/
		override protected function dragStartHandler(event:DragEvent):void{
			if(event.dragInitiator == this && event.localY < this.headerHeight){			
				event.preventDefault();
				return;
			}else{
				super.dragStartHandler(event);
			}
		}

		override protected function dragDropHandler(event:DragEvent):void{
			try{
				event.action = DragManager.MOVE;
				DragManager.showFeedback(DragManager.MOVE);
				super.dragDropHandler(event);
			}catch(err:Error){
				trace("An error was thrown in the UnscrollableDataGrid Drag and drop handler " + err.message);
			}
							
		}
		
		//Borrowed Code is Below except for comments which are my own rationalization
		/*when creating children also see if the footer exists.  If not make a new food with this style name and add it as a child.  This makes it so that the footer
		is always the last child*/		
	    override protected function createChildren():void
	    {
	        super.createChildren();
	
	        if (!footer)
	        {
	            footer = new AdvancedDataGridFooter();
	            footer.styleName = this;
	            addChild(footer);
	        }
	    }
	
		/*default width and height are - 1.  Pass work on to parent and adjust the base Display container known as listContent and set its size to be it's current size
		with the exception of the constant height of the footer.  Set the size of the footer to be this same size and move the footer to be exactly at the bottom of
		this display box. */
	    override protected function adjustListContent(unscaledWidth:Number = -1,
	                                       unscaledHeight:Number = -1):void
	    {
	        super.adjustListContent(unscaledWidth, unscaledHeight);
	
	        listContent.setActualSize(listContent.width, listContent.height - FOOTER_HEIGHT);
	        
	        footer.setActualSize(listContent.width, FOOTER_HEIGHT);
	        footer.move(listContent.x, listContent.y + listContent.height + 1);
	    }
	
		/*pass work on to parent, which marks this component ready to be rerendered at a later update call.  Generally this is called automatically when an item
		is visually changed such as by the adjustListContent function.  The additional work of this method is that it marks the footer to also be rerendered during
		the next update. */
	    override public function invalidateDisplayList():void
	    {
	        super.invalidateDisplayList();
	        if (footer)
	            footer.invalidateDisplayList();
	    }
	    
	    //End of borrowed code			
		
	}
}